package net.caixiaomi.info.widgets;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import net.caixiaomi.info.R;

import butterknife.BindView;
import butterknife.ButterKnife;

public class DefLoading extends AlertDialog implements DialogInterface.OnKeyListener {

    @BindView(R.id.tv_text)
    TextView mTitle;

    private String mContent;

    public DefLoading(Context context) {
        super(context, net.caixiaomi.info.R.style.myDialogTheme);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.def_loading);
        ButterKnife.bind(this);
        Window window = getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.dimAmount = 0f;
        this.setCanceledOnTouchOutside(false);
        setCancelable(false);
        setOnKeyListener(this);
    }


    public void setContent(String content) {
        mContent = content;
        if (!TextUtils.isEmpty(mContent) && mTitle != null) {
            mTitle.setText(mContent);
        }
    }

    @Override
    public void show() {
        super.show();
        if (!TextUtils.isEmpty(mContent)) {
            mTitle.setVisibility(View.VISIBLE);
            mTitle.setText(mContent);
        }
    }

    @Override
    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            dismiss();
        }
        return false;
    }
}
